using System;
using System.Data;
using System.IO;
using MbUnit.Framework;
using Mite.BuildTask;
using Mite.MsSql;

namespace Mite.Core.Tests.Integration
{
	[Author("Martin Murphy")]
	[TestFixture]
	public class TaskTester : MsSqlIntegrationTestBase
	{
		//This doesn't work in MbUnit for Some reason
		//[Test]
		//[ExpectedException(typeof (DirectoryNotFoundException))]
		//public void IMigrationTask_Migrate_Throws_Directory_Not_Found_Exception()
		//{
		//    TestMigrationTask task = new TestMigrationTask();
		//    task.Migrate();
		//}


		[Test]
		public void Assert_IMigrationTask_CSharp_Migrate_Works_With_Default_Path()
		{

			Environment.CurrentDirectory = AppDomain.CurrentDomain.BaseDirectory;

			var task = new TestMigrationTask();
			task.Migrate();
			MsSqlAdapter adapter = GetMsSqlAdapter();
			using (var conn = GetConnection())
			{
				conn.Open();
				Assert.IsTrue(
					adapter.GetDatabase(conn, DATABASE_NAME).Tables.Contains(t => t.Name == "Fruit"),
					"Migration Table not found");
				conn.Close();
			}

			Directory.Delete(Environment.CurrentDirectory + "\\migrations");
		}

		protected MsSqlAdapter GetMsSqlAdapter()
		{
			var adapter = new MsSqlAdapter();
			return adapter;
		}

		protected IDbConnection GetConnection()
		{
			var adapter = new MsSqlAdapter();
			var conn = adapter.CreateConnection(
				environment.Host,
				environment.Database,
				environment.Username,
				environment.Password,
				environment.Options);
			return conn;
		}
	}

	public class TestMigrationTask : IMigrationTask
	{
		#region IMigrationTask Members

		public string SelectedEnvironment
		{
			get { return "Test"; }
			set { throw new NotImplementedException(); }
		}

		public string MigrationDirectory
		{
			get
			{				
				return new DirectoryInfo(Environment.CurrentDirectory).Parent.Parent.FullName + "\\MsSql\\Dsl\\FruitStand";
			}
			set { throw new NotImplementedException(); }
		}

		public string EnvironmentSettingsFilePath
		{
			get
			{
				var di = new DirectoryInfo(Environment.CurrentDirectory);
				return di.Parent.Parent.FullName + @"\MsSql\settings.boo";
			}
			set { throw new NotImplementedException(); }
		}

		#endregion
	}
}